<?php
namespace app\api\controller\user;

// 底层控制器
use think\Controller;

//引入cookie
use think\Cookie;

class Recharge extends Controller {
    // 构造函数

    public function __construct() {
        // 手动把父类的方法获取过来
        parent::__construct();

        $this->UserModel = model( 'User.User' );
        $this->ConfigModel = model( 'Config' );
        $this->RechargeModel = model( 'User.Recharge' );
    }

    // 充值积分

    public function add() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $Tips = [
                'msg' => '',
                'result' => false,
            ];

            // 获取前端传来的数据
            $userid = $this->request->param( 'userid', '' );
            $point = $this->request->param( 'point', '' );

            // 判断用户是否存在
            $userInfo = $this->UserModel->where( [ 'id' => $userid ] )->find();

            // 判断用户是否存在
            if ( !$userInfo ) {
                $Tips[ 'msg' ] = '用户不存在！';
                return $Tips;
                exit;
            }

            // 判断充值金额是否为空
            if ( empty( $point ) ) {
                $Tips[ 'msg' ] = '充值金额不能为空！';
                return $Tips;
                exit;
            }

            // 判断该用户是否在最近5分钟由此操作
            $rechargeInfo = $this->RechargeModel->order( 'createtime', 'desc' )->find();

            $time = strtotime( 'now' ) - strtotime( $rechargeInfo[ 'createtime' ] );

            if ( $time/60 < 5 ) {
                $Tips[ 'msg' ] = '请在五分钟后，再次充值';
                return $Tips;
                exit;
            }

            // 组装数据
            $data = [
                'userid' => $userid,
                'point' => $point,
                'status' => '0',
            ];

            // 充值表插入操作
            $res = $this->RechargeModel->save( $data );

            if ( $res === false ) {
                $Tips[ 'msg' ] = '充值失败！';
                return $Tips;
                exit;
            } else {
                // 提交事务
                $Tips[ 'result' ] = true;
                $Tips[ 'msg' ] = '充值成功！等待审核！';
                return $Tips;
                exit;
            }

        }

    }

    // 获取充值记录

    public function index() {
        if ( $this->request->param() ) {
            // 获取前端传来的数据
            $userid = $this->request->param( 'userid', '' );
            $status = $this->request->param( 'status', '' );

            // 判断用户是否存在
            $userInfo = $this->UserModel->where( [ 'id' => $userid ] )->find();

            // 判断用户是否存在
            if ( !$userInfo ) {
                $this->error( '用户不存在！' );
                exit;
            }

            // 组装条件
            $where = [];
            $where[ 'userid' ] = $userid;
            if ( $status != '' ) {
                $where[ 'status' ] = intval( $status );
            }

            // 获取数据
            $rechargeInfo = $this->RechargeModel->where( $where )->order( 'createtime', 'desc' )->select();

            if ( $rechargeInfo === false ) {
                $this->error( '获取充值数据失败！' );
                exit;
            } else {
                $this->success( '获取充值数据成功！', '', $rechargeInfo );
                exit;
            }
        }

        return $rechargeInfo;
    }

    // 检查输入密码是否正确

    public function check() {
        // 提示信息
        $Tips = [
            'result' => false,
            'msg' => '',
        ];

        $userid = $this->request->param( 'id', 0 );
        $password = $this->request->param( 'password', '' );

        // 判断用户是否存在
        $userInfo = $this->UserModel->where( [ 'id' => $userid ] )->find();

        // 判断用户是否存在
        if ( !$userInfo ) {
            $Tips[ 'msg' ] = '用户不存在！';
            return $Tips;
            exit;
        }

        $password = md5( $password.$userInfo[ 'salt' ] );

        if ( $password != $userInfo[ 'password' ] ) {
            $Tips[ 'msg' ] = '密码错误！请重新输入！';
            return $Tips;
            exit;
        }

        $Tips[ 'result' ] = true;

        return $Tips;

    }
}
?>
